﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class Site
    {
        public long id { get; set; }
        public string name { get; set; }
    }
    public class SiteManagement
    {
        FosIntelEntities _en = new FosIntelEntities();
        
        public Site GetSite(string siteName)
        {
            fos_Site site = (from a in _en.fos_Site
                             where a.SiteName == siteName
                             select a).FirstOrDefault();

            Site siteobj = new Site();
            siteobj.id = site.Id;
            siteobj.name = site.SiteName;

            return siteobj;
        }
        public void SaveSite(fos_Site siteobj)
        {
            if (string.IsNullOrEmpty(siteobj.SiteName)) throw new Exception("Fields cannot be empty!");
            

            fos_Site site = (from a in _en.fos_Site
                             where a.SiteName == siteobj.SiteName && a.IsDeleted == 0
                             select a).FirstOrDefault();
            if (site != null)
                throw new Exception("Site already existed in database!");
            // Not activated yet
            siteobj.IsDeleted = 0;
            siteobj.CreatedBy = "Site Management";
            siteobj.CreatedDate = DateTime.Now;
            siteobj.LastModifiedDate = DateTime.Now;


            _en.fos_Site.Add(siteobj);
            _en.SaveChanges();
        }

        public void DeleteSite(long id)
        {
            
            fos_Site site = (from a in _en.fos_Site
                             where a.Id == id
                             select a).FirstOrDefault();
            if (site == null)
                throw new Exception("Site does not exist in database!");

            site.IsDeleted = 1;
            site.LastModifiedBy = "Site Management";
            site.LastModifiedDate = DateTime.Now;
            _en.SaveChanges();
            
        }

        public void UpdateSite(long id, string name)
        {
            fos_Site site = (from a in _en.fos_Site
                             where a.SiteName == name && (a.IsDeleted == 0 || a.IsDeleted == null)
                             select a).FirstOrDefault();
            if (site != null)
                throw new Exception("Site name already existed in system!");

            fos_Site siteUpdate = (from a in _en.fos_Site
                             where a.Id == id
                             select a).FirstOrDefault();

            siteUpdate.LastModifiedBy = "Site Management";
            siteUpdate.LastModifiedDate = DateTime.Now;
            siteUpdate.SiteName = name;
            _en.SaveChanges();
        }
        public List<Site> GetAllSite()
        {
            List<Site> siteList = new List<Site>();
            List<fos_Site> dbsiteList = (from a in _en.fos_Site
                             where a.IsDeleted == 0 || a.IsDeleted == null
                             select a).ToList();
            
            foreach(fos_Site site in dbsiteList)
            {
                Site existingSite = new Site();
                existingSite.id = site.Id;
                existingSite.name = site.SiteName;
                siteList.Add(existingSite);
            }

            return siteList;
        }
        
    }

}
